<?php

namespace App\Imports\Member;

use App\Country;
use App\Diseases;
use App\SeaArea;
use App\Strain;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;

class ImportMember  implements ToModel,WithStartRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {

        if (empty($row[0])){
            return null;
        }
        $time = date('Y-m-d H:i:s');
        $country_name = trim($row[0]);
        $sea_name = trim($row[1]);
        $disease_name = trim($row[2]);
        $contry = Country::where('name',$country_name)->first();
        if (empty($contry)){
            $contry = new Country();
            $contry->name = $country_name;
            $contry->save();
        }
        $sea = SeaArea::where('name',$sea_name)->first();
        if (empty($sea)){
            $sea = new SeaArea();
            $sea->name = $sea_name;
            $sea->save();
        }
        $disease = Diseases::where('name',$disease_name)->first();
        if (empty($disease)){
            $disease = new Diseases();
            $disease->name = $disease_name;
            $disease->save();
        }
        return new Strain([
            //
            'country_id' => $contry->id,
            'sea_area_id' => $sea->id,
            'diseases_id' => $disease->id,
            'strain_name' => $row[3]??'',
            'characteristics' => $row[4]??'',
            'drug' => $row[5]??'',
            'clinica' => $row[6]??'',
            'treatment' => $row[7]??'',
            'check' => $row[8]??'',
            'spread' => $row[9]??'',
            'score' => $row[10]??'',
            'remarks' => $row[11]??'',
        ]);
    }

    public function startRow(): int
    {
        return 2;
    }
}
